package com.chixing.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig implements BeanPostProcessor {
    //1. 创建交换机
    @Bean
    public DirectExchange newExchange(){
        return new DirectExchange("normalExchange",true,false);
    }

    //2. 创建队列
    @Bean
    public Queue newQueue(){
        Map<String ,Object> map = new HashMap<>();
        map.put("x-message-ttl",60000); // 消息存活时间60s
        map.put("x-dead-letter-exchange","deadExchange");// 设置死信交换机 的名称
        map.put("x-dead-letter-routing-key","key2") ;//设置死信路由键名字
        return new Queue("normalQueue",true,false,false,map);
    }

    //3. 绑定
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(newQueue()).to(newExchange()).with("key1");
    }

    //4. 创建死信交换机
    @Bean
    public DirectExchange newDeadExchange(){
        return new DirectExchange("deadExchange",true,false);
    }

    //5. 创建死信队列
    @Bean
    public Queue newDeadQueue(){
        return new Queue("deadQueue",true,false,false);
    }

    //6. 绑定
    @Bean
    public Binding bindingDead(){
        return BindingBuilder.bind(newDeadQueue()).to(newDeadExchange()).with("key2");
    }

    //7.支付交换机
    @Bean
    public DirectExchange payExchange(){
        return new DirectExchange("payExchange",true,false);
    }

    //8.支付队列
    @Bean
    public Queue payQueue(){
        return new Queue("payQueue",true,false,false);
    }

    //9.绑定
    @Bean
    public Binding bindingPay(){
        return BindingBuilder.bind(payQueue()).to(payExchange()).with("key3");
    }

}
